﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VQWEB.administrator.sources;
using VQData;
using System.Data;

namespace VQWEB.administrator.english.tests
{
    public partial class test_part_add : System.Web.UI.Page
    {
        EnglishData EnglishData = new EnglishData();
        eng_tests_part eng_tests_part = null;
        int Id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (RouteData.Values["Id"] != null)
                int.TryParse(RouteData.Values["Id"].ToString(), out Id);
            if (Id > 0)
            {
                frmTestPart.DefaultMode = FormViewMode.Edit;
                eng_tests_part = EnglishData.GetTestPartById(Id);
                //news = NewsData.GetNewsById(Id);
                frmTestPart.DataSource = new List<eng_tests_part> { eng_tests_part };
                frmTestPart.DataBind();
            }
            else frmTestPart.DefaultMode = FormViewMode.Insert;
        }

        protected void frmTestPart_ItemCreated(object sender, EventArgs e)
        {
            var test_control = frmTestPart.Row.FindControl("ltrTest") as Literal;
            test_control.Text = ShowTest();

            var group_control = frmTestPart.Row.FindControl("ltrGroup") as Literal;
            group_control.Text = ShowGroup(Id);

            var excercise_control = frmTestPart.Row.FindControl("ltrExcercise") as Literal;
            excercise_control.Text = ShowExcercise(Id);
        }

        private string ShowTest()
        {
            int Id = 0;
            if (eng_tests_part != null && eng_tests_part.test_id.HasValue)
            {
                Id = eng_tests_part.test_id.Value;
            }
            string html = string.Empty;
            DataTable table = EnglishData.ViewTests();
            if (table != null)
            {
                html += "<select name=\"test_id\" class=\"form-control\">";
                foreach (DataRow item in table.Rows)
                {
                    string selected = string.Empty;
                    int test_id = (int)item["test_id"];
                    string test_title = (string)item["test_title"];
                    if (Id == test_id)
                        selected = "selected=\"selected\"";
                    html += "<option " + selected + " value=" + test_id + ">" + test_title + "</option>";
                }
                html += "</select>";
            }
            return html;
        }

        private string ShowGroup(int part_test_id)
        {
            string html = string.Empty;
            DataTable table = EnglishData.ViewExcerciseGroups();
            if (table != null)
            {
                foreach (DataRow item in table.Rows)
                {
                    bool flag = false;
                    string check = string.Empty;
                    string group_excercise_title = (item["group_excercise_title"] != DBNull.Value) ? (string)item["group_excercise_title"] : "null";
                    int group_excercise_id = (int)item["group_excercise_id"];
                    string type_code = (item["type_code"] != DBNull.Value) ? (string)item["type_code"] : "null";
                    flag = EnglishData.CheckGroupInPartTest(group_excercise_id, part_test_id);
                    if (flag == true)
                        check = "checked=\"checked\"";
                    html += "<div class=\"checkbox\">";
                    html += "<label>";
                    html += "<input " + check + " name=\"group_array_id[]\" type=\"checkbox\" value=" + group_excercise_id + " />";
                    html += group_excercise_title + " [" + type_code + "]";
                    html += "</label>";
                    html += "</div>";
                }
            }
            return html;

        }

        private string ShowExcercise(int part_test_id)
        {
            string html = string.Empty;
            DataTable table = EnglishData.ViewExcercises();
            if (table != null)
            {
                foreach (DataRow item in table.Rows)
                {
                    bool flag = false;
                    string check = string.Empty;
                    string excercise_title = (string)item["excercise_title"];
                    int excercise_id = (int)item["excercise_id"];
                    string type_code = (item["type_code"] != DBNull.Value) ? (string)item["type_code"] : "null";
                    flag = EnglishData.CheckExcerciseInPartTest(excercise_id, part_test_id);
                    if (flag == true)
                        check = "checked=\"checked\"";
                    html += "<div class=\"checkbox\">";
                    html += "<label>";
                    html += "<input " + check + " name=\"excercise_array_id[]\" type=\"checkbox\" value=" + excercise_id + " />";
                    html += excercise_title + " [" + type_code + "]";
                    html += "</label>";
                    html += "</div>";
                }
            }
            return html;

        }
    }
}